package com.lewis.generator.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.lewis.core.base.domain.BaseEntity;
import com.lewis.core.utils.StringUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 * 代码生成业务字段表 gen_table_column
 *
 * @author Lewis
 */
@ApiModel("代码生成业务字段表 gen_table_column")
@EqualsAndHashCode(callSuper = true)
@Data
public class GenTableColumn extends BaseEntity {

	private static final long serialVersionUID = 1L;

	@ApiModelProperty(value = "编号")
	@TableId(value = "id", type = IdType.AUTO)
	private Long id;

	@ApiModelProperty(value = "归属表编号")
	private Long tableId;

	@ApiModelProperty(value = "列名称")
	private String columnName;

	@ApiModelProperty(value = "列描述")
	private String columnComment;

	@ApiModelProperty(value = "列类型")
	private String columnType;

	@ApiModelProperty(value = "JAVA类型")
	private String javaType;

	@ApiModelProperty(value = "JAVA字段名")
	@NotBlank(message = "Java属性不能为空")
	private String javaField;

	@ApiModelProperty(value = "是否主键（1是）")
	private String isPk;

	@ApiModelProperty(value = "是否自增（1是）")
	private String isIncrement;

	@ApiModelProperty(value = "是否必填（1是）")
	private String isRequired;

	@ApiModelProperty(value = "是否为插入字段（1是）")
	private String isInsert;

	@ApiModelProperty(value = " 是否编辑字段（1是）")
	private String isEdit;

	@ApiModelProperty(value = "是否列表字段（1是）")
	private String isList;

	@ApiModelProperty(value = "是否查询字段（1是）")
	private String isQuery;

	@ApiModelProperty(value = "查询方式（EQ等于、NE不等于、GT大于、LT小于、LIKE模糊、BETWEEN范围）")
	private String queryType;

	@ApiModelProperty(value = "显示类型（input文本框、textarea文本域、select下拉框、checkbox复选框、radio单选框、datetime日期控件、image图片上传控件、upload文件上传控件、editor富文本控件）")
	private String htmlType;

	@ApiModelProperty(value = "字典类型")
	private String dictType;

	@ApiModelProperty(value = "排序")
	private Integer sort;

	@ApiModelProperty(value = "创建者")
	private String createBy;

	@ApiModelProperty(value = "创建时间")
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date createTime;

	@ApiModelProperty(value = "更新者")
	private String updateBy;

	@ApiModelProperty(value = "更新时间")
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date updateTime;

	public static boolean isSuperColumn(String javaField) {
		return StringUtil.equalsAnyIgnoreCase(javaField,
				// BaseEntity
				"createBy" , "createTime" , "updateBy" , "updateTime" , "remark" ,
				// TreeEntity
				"parentName" , "parentId" , "orderNum" , "ancestors");
	}

	public boolean isPk() {
		return isPk(this.isPk);
	}

	public boolean isPk(String isPk) {
		return isPk != null && StringUtil.equals("1" , isPk);
	}

	public boolean isIncrement(String isIncrement) {
		return isIncrement != null && StringUtil.equals("1" , isIncrement);
	}

	public boolean isRequired() {
		return isRequired(this.isRequired);
	}

	public boolean isRequired(String isRequired) {
		return isRequired != null && StringUtil.equals("1" , isRequired);
	}

	public boolean isInsert() {
		return isInsert(this.isInsert);
	}

	public boolean isInsert(String isInsert) {
		return isInsert != null && StringUtil.equals("1" , isInsert);
	}

	public boolean isEdit() {
		return isInsert(this.isEdit);
	}

	public boolean isEdit(String isEdit) {
		return isEdit != null && StringUtil.equals("1" , isEdit);
	}

	public boolean isList() {
		return isList(this.isList);
	}

	public boolean isList(String isList) {
		return isList != null && StringUtil.equals("1" , isList);
	}

	public boolean isQuery() {
		return isQuery(this.isQuery);
	}

	public boolean isQuery(String isQuery) {
		return isQuery != null && StringUtil.equals("1" , isQuery);
	}

	public boolean isSuperColumn() {
		return isSuperColumn(this.javaField);
	}

	public String readConverterExp() {
		String remarks = StringUtil.substringBetween(this.columnComment, "（" , "）");
		StringBuilder sb = new StringBuilder();
		if (StringUtil.isNotEmpty(remarks)) {
			for (String value : remarks.split(" ")) {
				if (StringUtil.isNotEmpty(value)) {
					Object startStr = value.subSequence(0, 1);
					String endStr = value.substring(1);
					sb.append("").append(startStr).append("=").append(endStr).append(",");
				}
			}
			return sb.deleteCharAt(sb.length() - 1).toString();
		} else {
			return this.columnComment;
		}
	}
}
